//
//  RepairHistoryListViewController.m
//  wtshoppingcite
//
//  Created by Baojia-LC on 14/11/4.
//  Copyright (c) 2014年 TW. All rights reserved.
//

#import "RepairHistoryListViewController.h"
#import "RepairEditAndAddViewController.h"
#import "CustomTableView.h"
#import "NetServiseCommonData.h"
#import "RepairHistoryDetailViewController.h"
#import "RepairModel.h"
#import "ComplaintModel.h"

@interface RepairHistoryListViewController ()<UITableViewEventsDelegate,CustomTableViewDelegate>
{
    NSString *pageNum;
}
@property(nonatomic,strong)CustomTableView* tableView;
@property(nonatomic,strong)NSArray* dataSourse;
@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation RepairHistoryListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _dataArray = [[NSMutableArray alloc]initWithCapacity:1];
    
    if(_type == NSBlueViewControllerRepairStyle){
        
        _tableView = [[CustomTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHeight- 22) type:TABLE_VIEW_TYPE_REPAIRHISTORYLIST];
        
        [self setTitleView:@"报修记录"];
        [self setRightButton:@"报修" sel:@selector(repairClick:) viewController:self];
        
//        pageNum = @"0";
//        [self startDataRequest:NO];
        
    }else if(_type == NSBlueViewControllerComplainStyle){
        
        _tableView = [[CustomTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHeight - 22) type:TABLE_VIEW_TYPE_COMPLAINTLIST];
        
        [self setTitleView:@"投诉记录"];
        [self setRightButton:@"投诉" sel:@selector(repairClick:) viewController:self];
        
//        pageNum = @"0";
//        [self startComplaintDataRequest:NO];
        
    }else if(_type == NSBlueViewControllerPraiseStyle){
        [self setTitleView:@"表扬记录"];
        [self setRightButton:@"表扬" sel:@selector(repairClick:) viewController:self];
        
        pageNum = @"0";
        [self pullDown:_tableView];
    }

    //_tableView.opetationDelegate = self;
    _tableView.tableViewDelegate = self;
    _tableView.isNeedTorefresh = YES;
    _tableView.eventsDelegate = self;
    _tableView.isMore = YES;
    [self.view addSubview:_tableView];
    
    
}

#pragma mark  eventsDelegate
- (void)pullUp:(BaseTableView *)baseTableView{
    
    if (_type == NSBlueViewControllerRepairStyle) {
        
        [self startDataRequest:YES];
        
    }else if (_type == NSBlueViewControllerComplainStyle){
        
        [self startComplaintDataRequest:YES];
    }
    
}

- (void)pullDown:(BaseTableView *)baseTableView{
    
    pageNum = @"0";
    
    if (_type == NSBlueViewControllerRepairStyle) {
        
        [self startDataRequest:NO];
        
    }else if (_type == NSBlueViewControllerComplainStyle){
        
        [self startComplaintDataRequest:NO];
        
    }
   
}

#pragma mark 物业报修
-(void)startDataRequest:(BOOL)isMore{
    
    NSString *userId = [RegisterHelper shareHelper].userId;
    NSString *cid =[RegisterHelper shareHelper].citesId;
    NSString *page = pageNum;
    NSString *pageSize = @"10";
    
    NSMutableDictionary *listDic = [NSMutableDictionary dictionary];
    SetValueForKey(listDic, cid, @"cid");
    SetValueForKey(listDic, userId, @"userId");
    SetValueForKey(listDic, page, @"page");
    SetValueForKey(listDic, pageSize, @"pageSize");
    
    [self showLoadImageView:self.view subview:_tableView];
    [NetWorkService byRequestWithAPIPrefix:kBaseAppUrl
                                              andAPISuffix:kRepairsList
                                             andHttpMethod:@"POST"
                                             andParameters:listDic
                                           andRequestCache:NO
                                            andUseParamDic:YES
                                       andPostDataEncoding:MKNKPostDataEncodingTypeJSON
                                     andRequestFinishBlock:^(BYRequest *request, BYResponse *response) {
                                         [_tableView doneLoadingTableViewData];
                                         [self performSelector:@selector(dismissLoadImageView) withObject:nil afterDelay:0.5];
                                         request.requestCache = YES;
                                         if (response.isError) {
                                             
                                             DLog(@"error %@",response.errorMessage);
                                             
                                         }else{
                                             
                                             NSDictionary *responseDic = request.responseJSON;
                                             DLog(@"成功数据:%@",responseDic);
                                             
                                             NSArray *list = [responseDic objectForKey:@"list"];
                                             if (list.count < 10) {
                                                 _tableView.isMore = NO;
                                             }else{
                                                 _tableView.isMore = YES;
                                                 
                                                 int currentPage = [pageNum intValue];
                                                 pageNum = [NSString stringWithFormat:@"%d",currentPage+1];
                                                 
                                             }
                                             if (!isMore) {
                                                 
                                                 [_dataArray removeAllObjects];
                                                 [ _tableView.tableViewDataSource removeAllObjects];
                        
                                             }
                                             
                                             for (NSDictionary *dic in list) {
                                                 RepairModel *repairModel = [[RepairModel alloc]initWithRepairList:dic];
                                                 [_dataArray addObject:repairModel];
                                             }
                                             
                                             [_tableView clearAllTableViewData];
                                             _tableView.tableViewDataSource = _dataArray;
                                             
                                         }
                                         [_tableView reloadData];
                                     }];

}

#pragma mark 物业投诉
-(void)startComplaintDataRequest:(BOOL)isMore{
    
    NSString *userId = [RegisterHelper shareHelper].userId;
    NSString *cid =[RegisterHelper shareHelper].citesId;
    NSString *page = pageNum;
    NSString *pageSize = @"10";
    
    NSMutableDictionary *listDic = [NSMutableDictionary dictionary];
    SetValueForKey(listDic, cid, @"cid");
    SetValueForKey(listDic, userId, @"userId");
    SetValueForKey(listDic, page, @"page");
    SetValueForKey(listDic, pageSize, @"pageSize");
    
    [self showLoadImageView:self.view subview:_tableView];
    [NetWorkService byRequestWithAPIPrefix:kBaseAppUrl
                              andAPISuffix:kComplaintList
                             andHttpMethod:@"POST"
                             andParameters:listDic
                           andRequestCache:NO
                            andUseParamDic:YES
                       andPostDataEncoding:MKNKPostDataEncodingTypeJSON
                     andRequestFinishBlock:^(BYRequest *request, BYResponse *response) {
                         [_tableView doneLoadingTableViewData];
                         [self performSelector:@selector(dismissLoadImageView) withObject:nil afterDelay:0.5];
                         request.requestCache = YES;
                         if (response.isError) {
                             
                             DLog(@"error %@",response.errorMessage);
                             
                         }else{
                             
                             NSDictionary *responseDic = request.responseJSON;
                             DLog(@"成功数据:%@",responseDic);
                             
                             NSArray *list = [responseDic objectForKey:@"list"];
                             
                             
                             if (list.count < 10) {
                                 _tableView.isMore = NO;
                             }else{
                                 _tableView.isMore = YES;
                                 int currentPage = [pageNum intValue];
                                 pageNum = [NSString stringWithFormat:@"%d",currentPage+1];
                                 
                             }
                             if(!isMore){
                                 [_dataArray removeAllObjects];
                                 [ _tableView.tableViewDataSource removeAllObjects];
                             }

                             for (NSDictionary *dic in list) {
                                 ComplaintModel *repairModel = [[ComplaintModel alloc]initWithComplaintList:dic];
                                 [_dataArray addObject:repairModel];
                             }
                             
                             [_tableView clearAllTableViewData];
                             _tableView.tableViewDataSource = _dataArray;
                         }
                         
                         [_tableView reloadData];
                     }];
}

-(void)tableViewSelectedWithCellData:(id)cellData indexPath:(NSIndexPath *)indexPath tableView:(CustomTableView *)tableView{
    
    RepairHistoryDetailViewController* repairHistoryDetail = [[RepairHistoryDetailViewController alloc] init];
    
    if (_type == NSBlueViewControllerRepairStyle) {
        
        RepairModel *repairModel = _dataArray[indexPath.row];
        repairHistoryDetail.type = NSBlueViewControllerRepairStyle;
        repairHistoryDetail.repairsId = repairModel.repairId;
        repairHistoryDetail.state = repairModel.state;
       
    }else if (_type == NSBlueViewControllerComplainStyle){
        
        ComplaintModel *complaintModel = _dataArray[indexPath.row];
        repairHistoryDetail.type = NSBlueViewControllerComplainStyle;
        repairHistoryDetail.complaintId = complaintModel.complaintId;
        repairHistoryDetail.state = complaintModel.state;

    }
    
    [self.navigationController pushViewController:repairHistoryDetail animated:YES];
}

-(void)repairClick:(UIButton*)button{
    
    RepairEditAndAddViewController* repairEaVC = [[RepairEditAndAddViewController alloc] init];
    repairEaVC.hidesBottomBarWhenPushed = YES;
    repairEaVC.type = _type;
    [self.navigationController pushViewController:repairEaVC animated:YES];
        
  
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     
    [self pullDown:_tableView];
     
    ;
}


- (void)viewWillDisappear:(BOOL)animated
{
    
    [super viewWillDisappear:animated];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
